Skip to content

fix(android): scope the FileProvider and sanitise picked display names - #303

Merged
DeepDiver1975 merged 2 commits into
mainfrom
fix/review-android-fileprovider
Sep 11, 2026
Merged

DeepDiver1975 merged 2 commits into
mainfrom
fix/review-android-fileprovider

Conversation

@DeepDiver1975

Copy link
Copy Markdown
Contributor

Problem

  • file_paths.xml exposed the whole app cache directory through the FileProvider (path="."). That directory also holds the imported master-data JSON, exported PDFs, every picked attachment and the joined-client attachment cache, so any URI handed out by share or open could address any of them.
  • Picked attachments were copied to Path.Combine(cacheDir, displayName) with the content provider's DISPLAY_NAME used verbatim. A malicious provider can return a name with path separators and write outside the cache directory.

Fix

Found during a security review of main; part of a series of review follow-up PRs.

🤖 Generated with Claude Code

https://claude.ai/code/session_01PXhXSh2pae9WnaACYUUe3V

@DeepDiver1975

Copy link
Copy Markdown
Contributor Author

@claude review

@claude

claude Bot commented Sep 10, 2026 •

Copy link
Copy Markdown

Claude finished @DeepDiver1975's task in 2m 17s —— View job


Review: fix(android): scope the FileProvider and sanitise picked display names

  • Gather context (diff, changed files)
  • Understand request (@claude review)
  • Review file_paths.xml
  • Review AndroidAppPaths.cs
  • Review AndroidFileDialogService.cs
  • Review SafeFileName.cs + tests
  • Review CHANGELOG.md
  • Post final review feedback

Summary

The core fix is solid: file_paths.xml now exposes only shared/ and lagebuch/ instead of the whole cache root, picked/import files live in an unexposed picked/ dir, and EnsureShareable in AndroidFileDialogService.cs:200-221 is a clean single choke point that copies anything outside the two exposed roots into a fresh shared/<guid>/ folder before it ever reaches FileProvider.GetUriForFile. SafeFileName.Sanitize (src/LageBuch.AppLogic/Services/SafeFileName.cs) correctly normalises backslashes before Path.GetFileName, so both ../ and ..\ traversal attempts collapse to the final segment. Ran the new suite locally — all pass:

Passed! - Failed: 0, Passed: 15, Skipped: 0, Total: 15 - LageBuch.AppLogic.Tests.dll

IsUnder (AndroidFileDialogService.cs:223-227) appends the directory separator before the prefix check, which correctly avoids the classic "shared-evil".StartsWith("shared") false-positive — good attention to detail.

Minor: duplicated "lagebuch" literal, no shared constant

AndroidAppPaths.cs centralises SharedDir and PickedDir as named helpers, but the second exposed root is not centralised the same way — it's inlined in AndroidFileDialogService.cs:209:

var attachmentsDir = System.IO.Path.Combine(AndroidAppPaths.CacheDir(_activity), "lagebuch");

The string "lagebuch" now lives in three places: file_paths.xml's <cache-path name="attachments" path="lagebuch/" />, the doc comment in AndroidAppPaths.SharedDir (AndroidAppPaths.cs:34), and this inline Path.Combine. The PR body explicitly notes this protection "holds regardless of the merge order with #285" — but that guarantee depends on this literal staying in sync with whatever folder name #285's desktop-side fix actually lands on. Since AndroidAppPaths.cs already exists as the single source of truth for the other paths, adding e.g. AndroidAppPaths.AttachmentsOpenDir(Context) and using it here (and cross-referencing it from the XML comment) would remove the drift risk rather than relying on comments to keep three spots in sync.

Fix this →

Nit: silent overwrite on attachment-name collision

CompleteAttachment (AndroidFileDialogService.cs:109-132) writes to Path.Combine(AndroidAppPaths.PickedDir(_activity), DisplayNameOf(uri)). Since SafeFileName.Sanitize reduces names to their bare final segment, two different picked files that happen to sanitise to the same name (e.g. two photo.jpgs from different providers) will silently overwrite each other via File.Create. Not a security issue (app-private storage, no traversal), and may already be pre-existing behaviour from before this PR — just worth a second look if it isn't already handled downstream.

Other checked, no issues found

  • PickExportPdfAsync/PickExportJsonAsync correctly moved to SharedDir so exports line up with the newly-narrowed file_paths.xml.
  • CompleteImport correctly writes import.json into the unexposed PickedDir, not SharedDir.
  • AndroidManifest.xml's FileProvider declaration/grantUriPermissions is untouched and still points at @xml/file_paths, consistent with the narrowed paths file.
  • CHANGELOG.md's new Security entry accurately describes the fix.
  • Test file correctly covers ordinary names, ..//..\ traversal, embedded NUL, dot-only/empty/null inputs, and the caller-supplied fallback.

Nothing here blocks merging — the duplicated-literal point is a maintainability suggestion, not a correctness bug in this PR as written.

DeepDiver1975 and others added 2 commits September 11, 2026 11:59
The FileProvider previously exposed the entire app cache directory
(file_paths.xml's shared_cache pointed at "."), so any granted
content:// URI could in principle address import.json, picked
attachments, or the synced-attachment cache alongside the files this
app actually intends to share or open. It now exposes exactly two
subtrees: shared/ (export/share/open targets, via the new
AndroidAppPaths.SharedDir) and lagebuch/ (kept for the sibling
attachment-open PR, which writes there via Path.GetTempPath() on
Android).

Picked attachments and the imported master-data JSON move to a new,
unexposed AndroidAppPaths.PickedDir instead of landing loose in the
cache root.

DisplayNameOf also trusted a content provider's DISPLAY_NAME verbatim
when building the destination path for a picked attachment, letting a
malicious provider write outside the intended directory via a
"../"-style name. The new LageBuch.AppLogic.Services.SafeFileName
sanitiser (normalise backslashes, take the last path segment, strip
platform-invalid characters, fall back to "anhang") closes that off
and is unit-tested in isolation since there is no Android test
project.

Task 4 (fix/review-attachment-names, PR pending) is not yet merged
into this branch's base, so AttachmentTempPaths does not exist here
yet; OpenFileAsync gating on it is left to a follow-up once that PR
lands.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PXhXSh2pae9WnaACYUUe3V
Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com>
…efensively

Review finding: on this branch's base, FilesViewModel.OpenFileAsync hands
Path.Combine(Path.GetTempPath(), row.FileName) to IFileDialogService.OpenFileAsync,
which on Android resolves to the bare cache root -- no longer under either
file_paths.xml root after the previous commit narrowed exposure. That made
FileProvider.GetUriForFile throw IllegalArgumentException (uncaught) for every
attachment open until the sibling attachment-open PR (#285) lands and relocates
that write into the cache dir's "lagebuch" subtree.

AndroidFileDialogService.EnsureShareable is now the sole choke point between a
filesystem path and the FileProvider: both ShareFileAsync and OpenFileAsync route
through it instead of calling FileProvider.GetUriForFile on a caller-supplied path
directly. A path already under SharedDir or under <cache>/lagebuch/ passes through
untouched (so the PDF/JSON export paths, which already write straight into
SharedDir, incur no second copy); anything else is copied into a fresh SharedDir
subfolder first. A non-existent or non-regular-file source is refused (no launch)
rather than handed to the provider. This makes the regression structurally
impossible regardless of what any caller -- present or future -- passes in.

Also: SafeFileName.DefaultFallback is now public so DisplayNameOf can return it
directly instead of calling Sanitize(null) just to get the fallback.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01PXhXSh2pae9WnaACYUUe3V
Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com>
@DeepDiver1975
DeepDiver1975 force-pushed the fix/review-android-fileprovider branch from f09a748 to 85eda69 Compare September 11, 2026 10:01
@DeepDiver1975
DeepDiver1975 merged commit d28a41c into main Sep 11, 2026
3 checks passed
@DeepDiver1975
DeepDiver1975 deleted the fix/review-android-fileprovider branch September 11, 2026 10:16
DeepDiver1975 added a commit that referenced this pull request Sep 17, 2026
SafeFileName.Sanitize (#303) and IncidentFile.SanitizeFileName (#285)
landed in the same release from two different PRs doing the same job --
reduce an untrusted name to a safe bare file name, fall back to "anhang" --
with rules that had already drifted apart. The AppLogic one stripped only
Path.GetInvalidFileNameChars(), which on Linux is NUL and '/' alone.

Extract FileNameSanitizer into Domain, the only project both can see, and
keep the layer-specific behaviour on top: Create throws, Rehydrate falls
back to the storage-style name, SafeFileName substitutes a fallback. The
one thing that genuinely differed -- what "nothing usable left" means --
stays with the caller; TrySanitize just reports it as null.

This tightens the Android picked-name path, which now also strips the
Windows-invalid set, control and format characters, caps at 255 UTF-8
bytes keeping the extension, rejects an all-dots name, and trims. Each of
those is a name that previously survived the pick and only broke later, on
a peer whose filesystem cared.

SafeFileName stays as a name rather than being deleted: the Android head's
concern reads as "give me something usable", not "tell me whether this
name survives".

Part of #302 (the SafeFileName/SanitizeFileName duplication noted in the
v0.5.0 merge sequence).


Claude-Session: https://claude.ai/code/session_01YavFrkz1GFaMNXbHQKsMyf

Signed-off-by: Thomas Müller <1005065+DeepDiver1975@users.noreply.github.com>
Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant